package com.yinet.yyqh.service.impl;

import com.yinet.common.core.text.Convert;
import com.yinet.common.utils.DateUtils;
import com.yinet.generator.domain.YinetYctk;
import com.yinet.wsyy.domain.WsyyQuantum;
import com.yinet.wsyy.mapper.WsyyQuantumMapper;
import com.yinet.wsyy.mapper.WsyySettingMapper;
import com.yinet.yyqh.domain.Yyqh;
import com.yinet.yyqh.domain.YyqhGuidance;
import com.yinet.yyqh.domain.YyqhReturn;
import com.yinet.yyqh.domain.YyqhReturnDomain;
import com.yinet.yyqh.mapper.YyqhGuidanceMapper;
import com.yinet.yyqh.mapper.YyqhMapper;
import com.yinet.yyqh.service.YyqhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 网上预约列表Service业务层处理
 *
 * @author yinet
 * @date 2019-09-02
 */
@Service
public class YyqhServiceImpl implements YyqhService {
    @Autowired
    private YyqhMapper yyqhMapper;
    @Autowired
    private WsyyQuantumMapper yyqhQuantumMapper;
    @Autowired
    private WsyySettingMapper yyqhSettingMapper;
    @Autowired
    private YyqhGuidanceMapper yyqhGuidanceMapper;

    /**
     * 查询网上预约列表列表
     *
     * @param yyqh 网上预约列表
     * @return 网上预约列表
     */
    @Override
    public List<Yyqh> selectYyqhList(Yyqh yyqh) {
        return yyqhMapper.selectYyqhList(yyqh);
    }

    /**
     * 查询网上预约列表列表
     *
     * @param yyqh 网上预约列表
     * @return 网上预约列表
     */
    @Override
    public List<Yyqh> selectYyqhListByCard(Yyqh yyqh) {
        List<Yyqh> yyqhs = yyqhMapper.selectYyqhListByCard(yyqh);
        Collections.reverse(yyqhs);
        List<Yyqh> yyqhs1 = new ArrayList<>();
        for (Yyqh item : yyqhs) {
            WsyyQuantum wsyyQuantum = yyqhQuantumMapper.selectWsyyQuantumById(Long.parseLong(item.getBookedTime()));
            String timeQuantum = wsyyQuantum.getTimeQuantum();
            int startTime = Integer.parseInt(timeQuantum.substring(0, 2));
            int entTime = Integer.parseInt(timeQuantum.substring(6, 8));
            try {
                Date nowDate = new Date();
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date parse = df.parse(DateUtils.parseDateToStr("yyyy-MM-dd", item.getBookedDay()) + " " + entTime + ":00:00");
                long l = parse.getTime() - nowDate.getTime();
                if (l > 0) {
                    item.setBookedTime(wsyyQuantum.getTimeQuantum());
                    yyqhs1.add(item);
                }
            } catch (ParseException e) {
                System.err.println(e);
            }
        }
        return yyqhs1;
    }

    /**
     * 查询网上预约列表列表
     *
     * @param yyqh 网上预约列表
     * @return 网上预约列表
     */
    @Override
    public List<Yyqh> selectYyqhListExcel(Yyqh yyqh) {
        return yyqhMapper.selectYyqhListZh(yyqh);
    }

    /**
     * 新增网上预约列表
     *
     * @param yyqh 网上预约列表
     * @return 结果
     */
    @Override
    public int insertYyqh(Yyqh yyqh) {
        if (yyqhMapper.selectByIdNumforData(yyqh.getIdNum(), yyqh.getBookedDay(), yyqh.getBookedTime()) > 0) {
            return 3;
        } else if (yyqhMapper.selectCountForTime(Long.parseLong(yyqh.getBookedTime()), yyqh.getBookedDay(), yyqh.getGuidanceId()) > yyqhSettingMapper.selectWsyySetting().getTimeScope()) {
            return 2;
        } else {
            yyqh.setYyqhId(UUID.randomUUID().toString().replaceAll("-", ""));
            yyqh.setStatus("1");
            yyqh.setSubmitTime(new Date());
            return yyqhMapper.insertYyqh(yyqh);
        }
    }


    /**
     * 删除网上预约列表对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteYyqhByIds(String ids) {
        return yyqhMapper.deleteYyqhByIds(Convert.toStrArray(ids));
    }

    @Override
    public LinkedHashMap<String, List<Integer>> selectTimeNum(Date bookedDay, Long guidanceParent) {
        List<WsyyQuantum> yyqhQuantums = yyqhQuantumMapper.selectWsyyQuantumList("1");
        LinkedHashMap<String, List<Integer>> map = new LinkedHashMap();
        for (WsyyQuantum item : yyqhQuantums) {
            int i = yyqhMapper.selectCountForTime(item.getWsyyQuantumId(), bookedDay, guidanceParent);
            String timeQuantum = item.getTimeQuantum();
            int i1 = Integer.parseInt(item.getWsyyQuantumId().toString());
            map.put(timeQuantum, new ArrayList<Integer>() {
                {
                    add(i1);
                    add(i);
                }
            });
            timeQuantum = null;
        }
        return map;
    }

    @Override
    public YyqhReturn getCardForm(String Card) {
        YyqhReturn yyqhReturn = new YyqhReturn();
        try {
            List<Yyqh> yyqhs = yyqhMapper.selectYyqhListByIdNum(Card);

            if (yyqhs.size() == 0) {
                yyqhReturn.setState("1002");
                yyqhReturn.setContent("未查到人员信息");
            } else {
                Yyqh yyqh1 = yyqhs.get(0);
                WsyyQuantum wsyyQuantum = yyqhQuantumMapper.selectWsyyQuantumById(Long.parseLong(yyqh1.getBookedTime()));
                String timeQuantum = wsyyQuantum.getTimeQuantum();
                int startTime = Integer.parseInt(timeQuantum.substring(0, 2));
                int entTime = Integer.parseInt(timeQuantum.substring(6, 8));
                Date date = new Date();
                int hours = date.getHours();
                boolean sameDay = DateUtils.isSameDay(yyqh1.getBookedDay(), date);
                boolean isNowTimeHours = hours < entTime && hours >= startTime;
                if (sameDay && isNowTimeHours) {
                    yyqhReturn.setState("1");
                    yyqhReturn.setContent("查找成功");
                    List<YyqhReturnDomain> list = new ArrayList<>();
                    for (Yyqh item : yyqhs) {
                        YyqhGuidance parentItem = yyqhGuidanceMapper.selectGuidanceById(item.getGuidanceId());
                        YyqhReturnDomain yyqhReturnDomain = new YyqhReturnDomain();
                        yyqhReturnDomain.setID(item.getYyqhId());
                        yyqhReturnDomain.setCard(item.getIdNum());
                        yyqhReturnDomain.setKeShi(parentItem.getGuidanceName());
                        yyqhReturnDomain.setName(item.getApplyForName());
                        yyqhReturnDomain.setPhone(item.getPhongNum());
                        yyqhReturnDomain.setShiXiang("受理" + item.getGuidanceName() + "事项");
                        yyqhReturnDomain.setYuYueDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getBookedDay()));
                        if (entTime > 12) {
                            yyqhReturnDomain.setYuYueTime(timeQuantum);
                        } else {
                            yyqhReturnDomain.setYuYueTime(timeQuantum);
                        }
                        yyqhReturnDomain.setSequenceId(parentItem.getSequenceId());
                        list.add(yyqhReturnDomain);
                    }
                    yyqhReturn.setList(list);
                } else {
                    yyqhReturn.setState("1001");
                    yyqhReturn.setContent("取票的时间不在预约的时间段内或日期内");
                }

            }

        } catch (Exception e) {
            yyqhReturn.setState("9999");
            yyqhReturn.setContent("查找失败");
            System.out.println(e.getMessage());
        }
        return yyqhReturn;
    }

    @Override
    public YyqhReturn changeStatus(String id, String tickNum) {
        YyqhReturn yyqhReturn = new YyqhReturn();
        try {
            int i = yyqhMapper.changeStatus(id, tickNum);
            if (i == 1) {
                yyqhReturn.setState("1");
                yyqhReturn.setContent("更新成功");
            } else {
                yyqhReturn.setState("9999");
                yyqhReturn.setContent("更新失败");
            }
        } catch (Exception e) {
            yyqhReturn.setState("9999");
            yyqhReturn.setContent("更新失败");
            System.out.println(e.getMessage());
        }
        return yyqhReturn;
    }

    @Override
    public YyqhReturn cancelYy(String id) {
        YyqhReturn yyqhReturn = new YyqhReturn();
        int i = yyqhMapper.cancelYy(id);
//        int i = 1;
        if (i == 1) {
            yyqhReturn.setState("1");
            yyqhReturn.setContent("更新成功");
        } else {
            yyqhReturn.setState("9999");
            yyqhReturn.setContent("更新失败");
        }
        return yyqhReturn;
    }


}
